package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.stereotype.Component;
import org.springframework.core.convert.converter.Converter;

/**
 * @BelongsProject: lease
 * @BelongsPackage: com.atguigu.lease.web.admin.custom.config.converter
 * @Author: 王席舒
 * @CreateTime: 2024-09-29  14:17
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {


    //source  返回1 2 不是1和2抛异常
    @Override
    public ItemType convert(String source) {

        ItemType[] values = ItemType.values();

        for (ItemType value : values) {
            if (value.getCode().equals(Integer.valueOf(source))){
                return value;
            }
        }
        throw  new IllegalArgumentException("非法code!不是1和2!");
    }
}
